﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using GateKeeper;
using System.Globalization;

public partial class GateKeeper_User_Controls_IPWhiteList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        gridIPAddress.RowDataBound += new GridViewRowEventHandler(gridIPAddress_RowDataBound);
        btnAdd.Click += new EventHandler(btnAdd_Click);

        if (!Page.IsPostBack)
        { LoadData(); }
    }

    void gridIPAddress_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Dynamically hide the ID field and date so it's in the client data but not displayed
        e.Row.Cells[0].Visible = false;
        e.Row.Cells[1].Visible = false;

        // Dynamically add confirmation to delete button
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btn = (Button)e.Row.FindControl("btnDelete");
            if (btn != null) { btn.Attributes.Add("onClick", "return confirm('Are you sure you want to delete this entry?');"); }
        }
    }

    /// <summary>
    /// Binds to selected row and sends the id cell (0) to
    /// Utils.DeleteEntry for deletion based in the entryID
    /// </summary>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow grdRow = (GridViewRow)btn.Parent.Parent;
        Utils.DeleteEntry(XmlFileType.WhiteList, grdRow.Cells[0].Text);
        Response.Redirect(Request.RawUrl, true);
    }

    /// <summary>
    /// Adds new entry to the records list
    /// </summary>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtIPAddress.Text)) { return; }
        //Utils.AddIPAddressEntry(txtIPAddress.Text, txtComment.Text ?? string.Empty);
        Utils.AddEntry(XmlFileType.WhiteList, NodeType.ipaddress, txtIPAddress.Text, txtComment.Text ?? string.Empty);
        Response.Redirect(Request.RawUrl, true);
    }

    void LoadData()
    {
        List<ipAddress> entries = new List<ipAddress>();
        foreach (XmlNode node in Utils.xmlWhiteList.SelectSingleNode("/gatekeeper/ipaddress").ChildNodes)
        {
            ipAddress entry = new ipAddress();
            entry.ID = node.Attributes["id"].InnerText ?? string.Empty;
            entry.Date = DateTime.Parse(node.Attributes["date"].InnerText ?? string.Empty, CultureInfo.InvariantCulture);
            entry.Comment = node.Attributes["comment"].InnerText ?? string.Empty;
            entry.IPAddress = node.Attributes["ipaddress"].InnerText ?? string.Empty;

            // Make sure the ID and IPAddress are not empty - required fields
            if (!string.IsNullOrEmpty(entry.ID) && !string.IsNullOrEmpty(entry.IPAddress))
            { entries.Add(entry); }
        }
        entries.Sort(ipAddress.IPComparison);
        gridIPAddress.DataSource = entries;
        gridIPAddress.DataBind();
    }
}
